package com.example.yyy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_class_kt.*

class ClassKtActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_class_kt)
        var a = Animal();
        var ias = As("yyyy",0);
        println(ias.sexName);
        println(ias.getDesc("dwy"))
        println(judge("99"))
        btn_class_kt_hello.setOnClickListener {
            var count=0
            var sexBird = if (count++%3==0) Bird.MALE else Bird.FEMALE;
            var duck = Duck(sex = sexBird);
            tv_class_kt_hello.text = duck.getDesc("tt")
        }
        btn_class_kt_enum.setOnClickListener {
            var count:Int = 0;
            if (count%2 == 0){
                //ordinal表示枚举类型的序号,name表示枚举类型的名字
                tv_class_kt_hello.text = when(count++%4){
                    SeasonType.SPRING.ordinal -> SeasonType.SPRING.name
                    SeasonType.SUMMER.ordinal -> SeasonType.SUMMER.name
                    SeasonType.AUTUMN.ordinal -> SeasonType.AUTUMN.name
                    SeasonType.WINTER.ordinal -> SeasonType.AUTUMN.name
                    else -> "未知"
                }
            }else{
                tv_class_kt_hello.text = when(count++%4){
                    //自定义属性
                    SeasonName.SPRING.ordinal -> SeasonName.SPRING.seasonName
                    SeasonName.SUMMER.ordinal -> SeasonName.SUMMER.seasonName
                    SeasonName.AUTUMN.ordinal -> SeasonName.AUTUMN.seasonName
                    SeasonName.WINTER.ordinal -> SeasonName.AUTUMN.seasonName
                    else -> "未知"
                }
            }
        }
    }
    //通Java
    class As @JvmOverloads constructor(var name :String,val sex:Int ){
        var sexName:String;
        init {
            sexName=if (sex==0) "公" else "母";
        }

        fun getDesc(tag:String):String{
            return "this mo$sexName$tag"
        }
    }
    //静态
    companion object Wu {
        fun judge(sexName:String):Int{
            var sex:Int=when(sexName){
                "公","雄"->0
                "母","雌" ->1
                else->-1
            }
            return sex;
        }
    }
    //能被继承
    open class Bird (var name: String,val sex: Int= MALE){
        var sexName:String
        init {
            sexName=getsexName(sex)
        }

        private fun getsexName(sex: Int): String {
            TODO("Not yet implemented")
            return if (sex== MALE) "公" else "母"
        }
        fun getDesc(tag: String):String{
            return "欢迎来到$tag: 这${name}是${sexName}的"
        }
        companion object BirdStatic{
            val MALE = 0;
            val FEMALE = 1;
            val UNKNOWN = -1;

            fun judgeSex(sexName: String):Int {
                var sex: Int = when (sexName) {
                    "公", "雄" -> MALE;
                    "母", "雌" -> FEMALE;
                    else -> UNKNOWN;
                }
                return sex;
            }
        }
    }
    class Duck (name: String="yazi" ,sex: Int= BirdStatic.MALE):Bird(name){

    }
    //抽象类
    abstract class Chicken(name: String,sex: Int,var voice:String):Bird(name,sex){
        var numberArray:Array<String> = arrayOf("一","二","三","四","五","六","七","八","九","十")
        abstract fun CallOut(time: Int):String;
    }
    class Cook(name: String="鸡",sex: Int=Bird.MALE,voice: String="咯咯咯") : Chicken(name,sex,voice){
        override fun CallOut(time: Int): String {
            TODO("Not yet implemented")
            return "";
        }

    }
    //接口
    interface Ooo{
        var skilledSports: String;
        fun Swim(): String;
        fun Run(): String{
            return "跑"
        }
    }
    //接口代理
    class Boo : Ooo{
        override var skilledSports: String
            get() = TODO("Not yet implemented")
            set(value) {"bengpao"}

        override fun Swim(): String {
            TODO("Not yet implemented")
            return      "skilledSports$skilledSports"
        }

    }
    class Hen (name: String="小鸡", sex: Int=Bird.MALE):Bird(name,sex),Ooo {
        override var skilledSports: String
            get() = TODO("Not yet implemented")
            set(value) { "gghjhgf"}

        override fun Swim(): String {
            TODO("Not yet implemented")
            return "ggggggggg"
        }

        override fun Run(): String {
            return super.Run()
        }
    }
    class Hee (name: String="小鸡", sex: Int=Bird.MALE,ooo: Ooo=Boo()):Bird(name,sex),Ooo by ooo{

    }
    class Tree(treeName: String){
        init {

        }
        //嵌套类
        class Flower(flowerName: String){
            fun getName() :String{
                return "调用方法Tree.Flwoer(\"桃花\").getName"
            }
        }
        //内部类
         inner class Fruit(fruit: String){
            fun getName() :String{
                return "调用方法Tree(\"桃树\").Flwoer(\"桃子\").getName"
            }
        }
    }
    //枚举类
    enum class SeasonType{
        SPRING,SUMMER,AUTUMN,WINTER
    }
    enum class SeasonName(val seasonName:String){
        SPRING("春"),
        SUMMER("夏"),
        AUTUMN("秋"),
        WINTER("冬");
    }
    //数据类
    //自动死循get/set,自动提供equals,copy.toString
    data class Plant(var fruit: String,var flower: String,var name: String,var stem: String){}
    //模板类
    class River<T> (var name: String,var  length:T){
        fun getInfo():String{
            var unit:String = when(length){
                is String ->"米";
                is Number ->"m";
                else ->"";
            }
            return unit
        }
    }
}